package app

import (
	"flag"
	"github.com/rs/xid"
	"operator/pkg/config"
	"operator/pkg/util"
	"os"
	"strconv"
)

var sip string
var tip string
var version string
var port int

func ParseFlag() *config.SidecarConfig {
	flag.StringVar(&sip, "sip", "127.0.0.1", "server ip")
	flag.StringVar(&version, "version", "v1", "service version")
	flag.IntVar(&port, "port", 8080, "container port")
	flag.StringVar(&tip, "tip", "127.0.0.1", "trace ip")
	flag.Parse()

	if sip == "" {
		os.Exit(20)
	}

	podname := os.Getenv("POD_NAME")
	if podname == "" {
		podname = xid.New().String()
	}

	c := config.SidecarConfig{
		ServerAddr:    sip + ":" + strconv.Itoa(util.TcpServerPort),
		ContainerPort: port,
		Version:       version,
		ServiceName:   podname,
		ServiceIp:     tip,
	}

	return &c
}
